import type { ProColumns } from '@ant-design/pro-components';
import {EditableProTable, nanoid, PageContainer, ProForm, ProFormText} from '@ant-design/pro-components';
import {Form, message, Card, Typography, Row, Col, Space, Button, Upload, Modal} from 'antd';
import React, { useState } from 'react';
import { history } from 'umi';
import {
  DisciInfluenceApi,
  DownloadApi,
  FillApi, GetUploadedFiles, NoDataAPI,
  SchoolCheckShow, SchoolPass, SchoolReject,
  ShowApi,
  UploadApi, UploadWordPath
} from "@/services/School/SchoolApi";
import {ServerMessages} from "@/services/ServerMessages";
import {getYear} from "@/utils/format";
import {DownloadOutlined, ExclamationCircleOutlined, UploadOutlined} from "@ant-design/icons";
import Submitter from "@/pages/Syl/Common/Submitter";
import {DownloadByNameApi} from "@/services/Province/ProvinceApi";
import {ShowError} from "@/utils/format";
const { Text } = Typography;

export default () => {
  const status_is_show = (history.location.query.flag === 'display') || (history.location.query.id && history.location.query.fill_id)
  const status_is_check = history.location.query.flag === 'check'
  const is_seen = history.location.query.is_seen

  const handleCheck = () => {
    SchoolPass(history.location.query.id).then(response => {
      console.log(response)
      if (response.status === 0) {
        message.success(response.message)
        history.goBack()
      } else {
        ShowError(response.message)
      }
    })
    // history.goBack()
  }

  const { confirm } = Modal;


  const showConfirm = () => {
    confirm({
      title: '您确认驳回吗?',
      icon: <ExclamationCircleOutlined />,
      content: '驳回后，学科用户可重新填报。',
      onOk() {
        SchoolReject(history.location.query.id).then(response => {
          if (response.status === 0) {
            message.success(response.message)
            history.goBack()
          } else {
            ShowError(response.message)
          }
        }, () => {
          ShowError(ServerMessages.error_message)
        })
      },
      onCancel() {
        console.log('Cancel');
      },
    });
  };


  const handleBack = () => {
    showConfirm()
    // SchoolReject(history.location.query.fill_id).then(response => {
    //   if (response.status === 0) {
    //
    //   } else {
    //     ShowError(response.message)
    //   }
    // })
  }

  const path = []
  let loading = true
  const download = "3_1_1docx"

  const [downloadList, setDownloadList] = useState([]);


  return (
    <PageContainer header={{
      title: '学科师德师风建设情况',
      breadcrumb: {},
    }} content={<Text type="danger">2021年1月1日至今，学科在师德教育、宣传、考核、监督、奖励等方面取得的特色做法和显著成效以及师德师风负面清单等情况。<br/>仅填写当前填报周期内新增的数据，如有删减，请告知学校管理员。</Text>}
    >
      <Card bordered={false}>
        <ProForm
          request={() => {
            return new Promise(resolve => {
              if (status_is_check) {
                GetUploadedFiles('/api/index/school-query/review-download-query-wordname', history.location.query.id).then(response => {
                  if (response.status === 0) {
                    console.log(response.filenameList)
                    setDownloadList(response.filenameList)
                  }
                })
              } else if (status_is_show) {
                GetUploadedFiles('/api/wordname-query-download', download).then(response => {
                  if (response.status === 0) {
                    console.log(response.filenameList)
                    setDownloadList(response.filenameList)
                  }
                })
              }
              resolve(true)
            })
          }}
          onFinish={() => {
            return new Promise(resolve => {

              if (loading) {
                resolve(true)
              } else {
                console.log("@@path", path)
                UploadWordPath('/api/index/teacher-team/teacher-morality/honor-counts-word', path).then(response => {
                  console.log("@返回数据", response)
                  if (response.status === 0) {
                    message.success(response.message)
                    history.goBack()
                    resolve(true)
                  } else {
                    ShowError(response.message)
                    history.goBack()
                    resolve(true)
                  }
                }, () => {
                  ShowError(ServerMessages.error_message)
                  resolve(true)
                })
              }


            })
          }}
          hideRequiredMark
          style={{ margin: 'auto', marginTop: 8 }}
          name="basic"
          layout="horizontal"
          labelCol={{ span: 8 }}
          submitter={{
            render: (props, doms) => {
              if (status_is_check && is_seen === '0') {
                return <Row>
                  <Col offset={8}>
                    <Space>
                      <Button onClick={() => { history.goBack() }}>返回</Button>
                      <Button onClick={handleCheck} type="primary">通过</Button>
                      <Button danger onClick={handleBack}>驳回</Button>
                      <Button disabled>已提交</Button>
                    </Space>
                  </Col>
                </Row>
              } else if (status_is_check && is_seen === '1') {
                return <Row>
                  <Col offset={8}>
                    <Space>
                      <Button onClick={() => { history.goBack() }}>返回</Button>
                      <Button disabled>已审核</Button>
                    </Space>
                  </Col>
                </Row>
              } else if (status_is_show) {
                return (
                  <Row>
                    <Col offset={8}>
                      <Button onClick={() => { history.goBack() }}>返回</Button>
                      <Button disabled>已提交</Button>
                    </Col>
                  </Row>
                )
              }
              return (
                <Row>
                  <Col offset={8}>
                    <Space>
                      <Button onClick={() => { history.goBack() }}>返回</Button>
                      <Button onClick={() => {
                        NoDataAPI(download).then(res => {
                          if (res.status === 0) {
                            message.success(res.message)
                            history.goBack()
                          } else {
                            ShowError("提交失败，请稍后重试.")
                          }
                        }, () => {
                          ShowError(ServerMessages.error_message)
                        })
                      }
                      } type="default">无相关数据</Button>

                      <Button onClick={() => {
                        window.location.reload()
                      }}>重置</Button> {doms}</Space>
                  </Col>
                </Row>
              );
            },
            resetButtonProps: {
              style: {
                // 隐藏重置按钮
                display: 'none',
              },
            },
            submitButtonProps: {
              style: {
                marginLeft: '-8px'
              }
            },
          }}
        >
          {!status_is_show && (

            <Form.Item label="请上传相关文档">
            <Upload {...UploadApi('/api/index/teacher-team/teacher-morality/honor-counts-word-template', (response) => {
              try {
                path.push(response.file.response.data[0].path)
                console.log(path.length, response.fileList.length, path.length === response.fileList.length)
                if (path.length === response.fileList.length) {
                  console.log("@loading false!")
                  loading = false
                }
                if (path.length === 0) {
                  loading = true
                }
              } catch {
                ShowError('上传失败')
              }
            })}>
              <Button type="default" icon={<UploadOutlined/>}>上传文档</Button>
            </Upload>
          </Form.Item>
          )}
          {status_is_show && (
            <Space>
              {downloadList.map((item, idx) => {
                return <Button onClick={() => {
                  DownloadByNameApi(item)
                }
                } key={item}>下载文件{idx+1}</Button>
              })}
            </Space>
          )}
        </ProForm>
      </Card>
    </PageContainer>
  );
};
